"""
======================
Author: 柠檬班-小简
Time: 2022/11/2 20:23
Project: day8_framwork_V5
Company: 湖南零檬信息技术有限公司
======================
"""
import re
from tools import handle_genrate_data

req_data="""
{
 "t": #cur_time#,
 "prodName": "自动化#random_str#",
 "brief": "",
 "video": "",
 "prodNameEn": "sale something",
 "prodNameCn": "自动化#random_str#",
 "contentEn": "<p>https://item.jd.com/669048.html</p>",
 "contentCn": "<p>https://item.jd.com/669048.html</p>",
 "briefEn": "sale good something",
 "briefCn": "卖自动化知识",
 "pic": "#img_path#",
 "imgs": "#img_path#",
 "preSellStatus": 0,
 "preSellTime": null,
 "categoryId": 133,
 "skuList": [{
  "price": 0.01,
  "oriPrice": 0.01,
  "stocks": 100,
  "skuScore": 1,
  "properties": "",
  "skuName": "",
  "prodName": "",
  "weight": 0.1,
  "volume": 2,
  "status": 1,
  "partyCode": "py#cur_time#",
  "prodNameCn": "自动化#random_str#",
  "prodNameEn": "sale something"
 }],
 "tagList": [1],
 "content": "",
 "deliveryTemplateId": 1,
 "totalStocks": 100,
 "price": 0.01,
 "oriPrice": 0.01,
 "deliveryModeVo": {
  "hasShopDelivery": true,
  "hasUserPickUp": false,
  "hasCityDelivery": false
 }
}
"""

class Data:
    img_path = "123456767777.png"

# 利用正则表达式提取出占位符
to_be_replaced_mark_list = re.findall("#(\w+)#",req_data)
print(to_be_replaced_mark_list)

# 占位符的数据来自哪里？？ -- 响应结果中提取的，来自全局变量。 -- python脚本造的数据。
for mark in set(to_be_replaced_mark_list):
    print("要被替换的占位符是：", mark)
    # 判断是否在全局变量当中。如果是，替换掉
    if hasattr(Data, mark):
        req_data = req_data.replace(f"#{mark}#", getattr(Data, mark))
    elif mark in handle_genrate_data.__all__:
        # 如果不在全局变量当中，则去python脚本当中寻找同名的函数。
        # 调用模块当中的同名函数
        value = getattr(handle_genrate_data, mark)()
        req_data = req_data.replace(f"#{mark}#", str(value))
    else:
        print(f"标识符{mark}即不在全局变量中，也不在python造数据模块中，请确认标识符是否编写正常....")

print(req_data)